package com.yrot.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yrot.admin.entity.SysResult;
import com.yrot.admin.entity.SysResultDetails;
import com.yrot.admin.form.ResultAccuracyJson;
import com.yrot.admin.form.ResultDetailsJson;
import com.yrot.admin.form.ResultJson;
import com.yrot.admin.mapper.SysResultDetailsMapper;
import com.yrot.admin.mapper.SysResultMapper;
import com.yrot.admin.service.ISysResultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yrot.common.result.R;

import com.yrot.common.result.Result;
import com.yrot.common.result.ResultUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hemin
 * @since 2019-04-02
 */
@Service
public class SysResultServiceImpl extends ServiceImpl<SysResultMapper, SysResult> implements ISysResultService {

    @Autowired
    SysResultMapper resultMapper;
    @Autowired
    SysResultDetailsMapper resultDetailsMapper;
    @Autowired
    SysResultMapper sysResultMapper;

    @Override
    public SysResult findResultToday(Integer trainId,String identifier) {
        QueryWrapper<SysResult> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("train_id",trainId);
        queryWrapper.eq("identifier",identifier);
        SysResult one = getOne(queryWrapper);
        return one;
    }
    @Transactional
    @Override
    public R submitSave(SysResult result,String answer) {
        SysResult resultToday = findResultToday(result.getTrainId(), result.getIdentifier());
        if(resultToday!=null){
            if(resultToday.getScore()!=null){
                return R.error(0001,"该考生已经答过题，请勿重复提交");
            }
            resultToday.setScore(result.getScore()).setIsPass(result.getIsPass()).setUseTime(result.getUseTime());
            QueryWrapper<SysResult> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("train_id",result.getTrainId());
            queryWrapper.eq("identifier",result.getIdentifier());
            update(resultToday,queryWrapper);
            List<ResultDetailsJson> jsons = JSONObject.parseArray(answer, ResultDetailsJson.class);
            for(int i=0;i<jsons.size();i++){
                SysResultDetails details = new SysResultDetails();
                details.setResultId(resultToday.getResultId());
                BeanUtils.copyProperties(jsons.get(i),details);
                resultDetailsMapper.insert(details);
            }
            return R.ok(result.getResultId());
        }else{
            return R.error(0001,"该系统中没有考生！");
        }
    }

    @Override
    public Result getResultsByTrainId(int page, int limit, Integer trainId,String name,Integer isPass) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page-1);
        map.put("limit",limit);
        map.put("trainId",trainId);
        map.put("name",name);
        map.put("isPass",isPass);
        List<ResultJson> resultsByTrainId = sysResultMapper.findResultsByTrainId(map);
        QueryWrapper<SysResult> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("train_id",trainId);
        int count = count(queryWrapper);
        return ResultUtils.success(new Long(count), resultsByTrainId);
    }

    @Override
    public ResultAccuracyJson findAccuracy(Integer tranId) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("tranId",tranId);
        return resultMapper.findAccuracy(map);
    }
}
